﻿using System;
using System.Windows;

namespace ServerIntegration
{
    /// <summary>
    /// Interaction logic for ServerManagementWindow.xaml
    /// </summary>
    public partial class ServerManagementWindow : Window
    {
        private CharacterDownloadedReporter _characterDownloadedReporter;
        private SiteQuery _sq;

        public ServerManagementWindow()
        {
            InitializeComponent();
            _characterDownloadedReporter = new CharacterDownloadedReporter();
            CurrentLoginControl.Cancel_Button.Click += Cancel_Button_Click;
            CurrentLoginControl.Login_Button.Click += Login_Button_Click;
            CurrentCharacterBrowser.Visibility = Visibility.Collapsed;
            _characterDownloadedReporter.CloseRequested += CloseRequested;
        }


        internal void CloseRequested(object sender, EventArgs e)
        {
            _sq = null;
            this.Close();
        }


        public CharacterDownloadedReporter CharacterDownloadedReporter
        {
            get { return _characterDownloadedReporter; }
        }

        private void Login_Button_Click(object sender, RoutedEventArgs e)
        {
            var loginInfo = CurrentLoginControl.CurrentLoginInformation;
            _sq = new SiteQuery(loginInfo.TargetSite, loginInfo.Username, loginInfo.Password);
            _sq.Login();
            if (!_sq.LoginSuccessful)
            {
                LoginMessage_TextBlock.Text = "Error connecting";
            }
            else
            {
                LoginMessage_TextBlock.Visibility = Visibility.Collapsed;
                CurrentLoginControl.Visibility = Visibility.Collapsed;
                CurrentCharacterBrowser.SetSiteQuery(_sq);
                CurrentCharacterBrowser.SetCharacterDownloadedReporter(_characterDownloadedReporter);
                CurrentCharacterBrowser.Visibility = Visibility.Visible;
            }
        }

        private void Cancel_Button_Click(object sender, RoutedEventArgs e)
        {
        }
    }
}